/**
 * Created by HaiHui on 2017/10/17.
 */
var table;
$(document).ready(function() {
	table = $('#tomb').DataTable({
		"pagingType": "simple_numbers", //设置分页控件的模式
		searching: false, //屏蔽datatales的查询框
		aLengthMenu: [10], //设置一页展示10条记录
		"bLengthChange": false, //屏蔽tables的一页展示多少条记录的下拉列表
		"ordering": false, //屏蔽排序
		"oLanguage": { //对表格国际化
			"sLengthMenu": "每页显示 _MENU_条",
			"sZeroRecords": "没有找到符合条件的数据",
			//  "sProcessing": "&lt;img src=’./loading.gif’ /&gt;",
			"sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
			"sInfoEmpty": "木有记录",
			"sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
			"sSearch": "搜索：",
			"oPaginate": {
				"sFirst": "首页",
				"sPrevious": "前一页",
				"sNext": "后一页",
				"sLast": "尾页"

			}
		},
		"processing": true, //打开数据加载时的等待效果
		"serverSide": true, //打开后台分页
		"ajax": {
			"url": "dust/list",
			"dataSrc": "aaData",
			"data": function(d) {
				d.dustName = $('#selectDustName').val();
				//添加额外的参数传给服务器
				//d.aduserRealname = aduserRealname;
			}
		},
		"columns": [{
					"data": "dustId",
					"visible": false
				},
				{
					"data": "dustName",
					"title": "逝者姓名"
				},
				{
					"data": "areaName",
					"title": "身份证号"
				},
				{
					"data": "dustPersionName",
					"title": "家庭住址"
				},
				{
					"data": "dustPersionPhone",
					"title": "联系人"
				},
				{
					"data": "dustGraveCount",
					"title": "联系电话"
				},
				{
					"data": "dustOkGraveCount",
					"title": "火化时间"
				},			
				{
					"data": "dustRemark",
					"title": "死亡时间"
				},
				{
					"data": null,
					"title": "操作"
				}	
			],
			"columnDefs": [{
			"targets": -1, //操作按钮目标列
			"data": null,
			"defaultContent": "<i title=\"详情\" class=\"dogo-js-search fa fa-search\"></i><i title=\"添加费用\"  class=\"dogo-js-edit fa fa-rmb\"></i>"

		}],
	});
	//查看
	$('#tomb tbody').on('click', '.dogo-js-search', function() {
		var data = table.row($(this).parents('tr')).data();
		showDetail(data.dustId)
		// $('#search-modal').modal(data.prId);
	});
	//del
	$('#tomb tbody').on('click', '.dogo-js-del', function() {
		var data = table.row($(this).parents('tr')).data();
		var confirmIdx = layer.confirm("确认要删除?", {
				btn: ['删除', '取消']
			},
			function() {
				var setting = {
					url: "dust/delete",
					data: {
						id: data.dustId
					}
				}
				ajaxSubmit(setting, function(responseData) {
					checkSubmitResult(responseData, {
						error: "删除失败"
					}, function(responseData) {
						layer.alert(responseData.description);
						$('#tomb').DataTable().ajax.reload();

					});
				}, "删除失败");
			},
			function() {
				layer.close(confirmIdx);
			});

	});
	//edit
	$('#tomb tbody').on('click', '.dogo-js-edit', function() {

		var data = table.row($(this).parents('tr')).data();
		/*showEditModal(data.dustId);*/
		window.location.href="costedit.html?dustId="+data.dustId+"&item_nav=funeral";

	});
});

function search() {
	table.ajax.reload();

}

function showAddModal() {
	$("#modal-label").text("添加殡仪馆信息");
	$("#saveProjectInfo").text("添加");
	$("#saveProjectInfo").data("opType", "add");
	$("#dustId").val("");
	$("#dustName").val("");
	$("#dustPersionName").val("");
	$("#dustRegionId").val("");
	$("#dustPersionPhone").val("");
	$("#dustRemark").val("");
	$('#edit-modal').modal();
	//initCombo('dePrid', '/xbermanager/projects/list', 'prId', 'prPrname');
}

function showEditModal(dataId) {
	$("#modal-label").text("修改殡仪馆信息");
	$("#saveProjectInfo").text("修改");
	$("#saveProjectInfo").data("opType", "edit");
	var setting = {
		url: "dust/detail",
		data: {
			id: dataId
		}
	}
	ajaxSubmit(setting, function(responseData) {
		checkSubmitResult(responseData, {
			error: "获取殡仪馆信息失败"
		}, function(responseData) {
			//TODO 详情页面数据填充
			var data = responseData.data;
			$("#dustId").val(data.dustId);
			$("#dustName").val(data.dustName);
			$("#dustPersionName").val(data.dustPersionName);
			$("#dustPersionPhone").val(data.dustPersionPhone);
			$("#dustRemark").val(data.dustRemark);

			//initCombo('dePrid', '/xbermanager/projects/list', 'prId', 'prPrname', data.dustRegionId);

			$("#edit-modal").modal('show');
		});
	}, "获取殡仪馆信息失败");

}

function showDetail(dataId) {
	var setting = {
		url: "dust/detail",

		data: {
			id: dataId
		}
	}
	ajaxSubmit(setting, function(responseData) {
		checkSubmitResult(responseData, {
			error: "获取殡仪馆信息失败"
		}, function(responseData) {
			//TODO 详情页面数据填充
			var data = responseData.data;
			$("#search-modal").find("#infoDustName").text(data.dustName);
			$("#search-modal").find("#infoDustRemark").text(data.dustRemark);
			$("#search-modal").find("#infoDustPersionName").text(data.dustPersionName);
			$("#search-modal").find("#infoDustPersionPhone").text(data.dustPersionPhone);
			$("#search-modal").find("#infoDustGraveCount").text(data.dustGraveCount);
			$("#search-modal").find("#infoDustOkGraveCount").text(data.dustOkGraveCount);
			$("#search-modal").find("#infoDustNoGraveCount").text(data.dustNoGraveCount);
			$("#search-modal").find("#infoAreaName").text(data.areaName);
			$("#search-modal").modal('show');
		});
	}, "获取殡仪馆信息失败");
}
//添加、修改项目
$("#saveProjectInfo").click(function() {
	var opType = $(this).data("opType");
	var action = null;
	var formdata = {};
	//formdata = new FormData($("#editForm")[0]);

	formdata = $("#editForm").serializeJson()
	//formdata = $("#editForm").serializeArray();

	var errorMsg = null;

	if(opType == "add") {
		action = "dust/add";
		errorMsg = "添加信息失败";
	} else if(opType == "edit") {
		action = "dust/update";
		errorMsg = "修改信息失败";
	}

	if(action == null) {
		layer.alert("不支持的操作类型");
	} else {
		debugger;
		var setting = {
			url: action,
			data: JSON.stringify(formdata),
			cache: false,
			contentType: "application/json",
			processData: false,
			async: false

		}
		ajaxSubmit(setting, function(responseData) {
			checkSubmitResult(responseData, {
				error: errorMsg
			}, function(responseData) {
				$("#edit-modal").modal('hide');
				layer.alert(responseData.description);
				$('#tomb').DataTable().ajax.reload();

			});

		}, errorMsg);

	}
});